<template>
  <div>
    <p style="padding: 5px 15px 20px; font-size: 16px;">{{id !=-1 ? '新增':'修改'}}</p>
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="cKey" prop="cKey">
        <el-input v-model="dataForm.cKey" placeholder></el-input>
      </el-form-item>
      <el-form-item label="remark" prop="remark">
        <el-input v-model="dataForm.remark" placeholder></el-input>
      </el-form-item>
      <el-form-item label="cValue" prop="cValue">
        <el-input v-model="dataForm.cValue" placeholder></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="dataFormSubmit()">提交</el-button>
        <el-button size="small" @click="cancelEdit">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["id"],
  mounted() {
    this.getData();
  },
  data() {
    return {
      visible: false,
      dataForm: {
        cKey: "",
        remark: "",
        cValue: ""
      },
      dataRule: {
        cKey: [{ required: true, message: "不能为空", trigger: "blur" }],
        remark: [{ required: true, message: "不能为空", trigger: "blur" }],
        cValue: [{ required: true, message: "不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    getData(id) {
      this.$refs["dataForm"].resetFields();
      if (this.id !== -1) {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/miniapp/miniconfig/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.cKey = data.miniConfig.cKey;
              this.dataForm.remark = data.miniConfig.remark;
              this.dataForm.cValue = data.miniConfig.cValue;
            }
          });
        }
      }
    },
    cancelEdit() {
      this.$router.push({ name: "ag-system-config" });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/miniapp/miniconfig/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,

              cKey: this.dataForm.cKey,
              remark: this.dataForm.remark,
              cValue: this.dataForm.cValue
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
